#include "stdafx.h"
#include "ScriptEditor.h"
#include "ScriptEditorSolution.h"
#include "ScriptEditorWidget.h"
#include "ScriptEditorCompiler.h"
#include "ScriptEditorRegisters.h"

namespace Lit
{
	namespace Editor
	{
		ScriptEditor::ScriptEditor(QWidget *parent, Qt::WFlags flags)
			: QMainWindow(parent, flags)
		{
			Lit::FileSystem::createDirectories( Lit::Editor::ScriptUserPath().str() );
			ui.setupUi(this);;

			mInstance = new Instance( "ScriptEditor", this, ui.logDock );
			mVariables = new ScriptRegisters( this );

			mInstance->initialise( Lit::CoreComponentList() << COM_GAMESTATEMANAGER << COM_CACHEMANAGER << COM_SCENEMANAGER << COM_PHYSICSMANAGER, ui.ogreWidget, this );
		}

		ScriptEditor::~ScriptEditor()
		{
			mInstance->deInitialise( this );
			mSolution.destroy();
			mVariables.destroy();
			mInstance.destroy();
		}
			
		void ScriptEditor::createScene()
		{
			mSolution = new ScriptSolution( ui.treeWidget, ui.scriptTabs, mInstance );
			Ogre::Root::getSingleton().addFrameListener( this );
			updateUi();
		}
		bool ScriptEditor::frameStarted( const Ogre::FrameEvent &evt )
		{
			return Root::Instance().renderOneFrame( evt.timeSinceLastFrame );
		}
		void ScriptEditor::closeEvent( QCloseEvent * evt ){
			if( mSolution->closeSolution() )
				QMainWindow::closeEvent( evt );
			else evt->ignore();
		}
		void ScriptEditor::updateUi(){
			if( mSolution->isCreated() )
			{
				ui.actionSave_Solution->setEnabled( true );
				ui.actionSave_Solution_As->setEnabled( true );
				ui.actionClose_Solution->setEnabled( true );

				ui.actionNew_Script->setEnabled(true);
				ui.actionOpen_Script->setEnabled(true);
				ui.actionSave_Script->setEnabled(true);
				ui.actionSave_Script_As->setEnabled(true);
				ui.actionClose_Script->setEnabled(true);
				
				ui.actionCompile->setEnabled(true);
				ui.actionCompile_ByteCode->setEnabled(true);
				ui.actionCompile_Run->setEnabled(true);
			}
			else
			{
				ui.actionSave_Solution->setEnabled( false );
				ui.actionSave_Solution_As->setEnabled( false );
				ui.actionClose_Solution->setEnabled( false );

				ui.actionNew_Script->setEnabled(false);
				ui.actionOpen_Script->setEnabled(false);
				ui.actionSave_Script->setEnabled(false);
				ui.actionSave_Script_As->setEnabled(false);
				ui.actionClose_Script->setEnabled(false);
				
				ui.actionCompile->setEnabled(false);
				ui.actionCompile_ByteCode->setEnabled(false);
				ui.actionCompile_Run->setEnabled(false);
			}
		}

		
		void ScriptEditor::on_actionNew_Solution_triggered()
		{
			mSolution->newSolution();
			updateUi();
		}
		void ScriptEditor::on_actionOpen_Solution_triggered()
		{
			mSolution->openSolution();
			updateUi();
		}
		void ScriptEditor::on_actionSave_Solution_triggered()
		{
			mSolution->saveSolution(false);
		}
		void ScriptEditor::on_actionSave_Solution_As_triggered()
		{
			mSolution->saveSolution(true);
		}
		void ScriptEditor::on_actionClose_Solution_triggered()
		{
			mSolution->closeSolution();
			updateUi();
		}
		void ScriptEditor::on_actionNew_Script_triggered()
		{
			mSolution->createScript();
		}
		void ScriptEditor::on_actionOpen_Script_triggered()
		{
			mSolution->openScript();
		}
		void ScriptEditor::on_actionSave_Script_triggered()
		{
			ScriptEditorWidgetPtr editor(ui.scriptTabs->currentScript( ));
			if( editor )
				editor->saveScript();
		}
		void ScriptEditor::on_actionSave_Script_As_triggered()
		{
			ScriptEditorWidgetPtr editor(ui.scriptTabs->currentScript( ));
			if( editor )
				editor->saveScriptAs();
		}
		void ScriptEditor::on_actionClose_Script_triggered()
		{
			ui.scriptTabs->closeScript( ui.scriptTabs->currentScript() );
		}
		void ScriptEditor::on_actionExit_triggered()
		{
			close();
		}
		
		void ScriptEditor::on_actionVariables_triggered()
		{
			mVariables->exec(mSolution->getCompiler()->getEngine());
		}
		void ScriptEditor::on_actionCompile_triggered()
		{
			mSolution->compile();
		}
		void ScriptEditor::on_actionCompile_Run_triggered()
		{
			mSolution->run();
		}
		void ScriptEditor::on_actionCompile_ByteCode_triggered()
		{
			mSolution->compileByteCode();
		}
		void ScriptEditor::on_actionProperties_triggered()
		{
			mSolution->showSettings();
		}
	};
};